package com.erp.erp_ui.ShipmentPlan;

import com.alibaba.fastjson.JSONObject;
import com.erp.erp_entitys.Bank.BankEntity;
import com.erp.erp_entitys.Customer.CustomerEntity;
import com.erp.erp_entitys.Customer.CustomerExample;
import com.erp.erp_entitys.Purchase.ExportVo;
import com.erp.erp_entitys.Purchase.PurchaseProductVO;
import com.erp.erp_entitys.ShipmentPlan.*;
import com.erp.erp_entitys.ShipmentPlanLog.ShipmentPlanLog;
import com.erp.erp_entitys.ShipmentPlanLog.ShipmentPlanLogExample;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_entitys.business.entity.CarryDetailProductEntity;
import com.erp.erp_servers.Bank.IBankService;
import com.erp.erp_servers.Company.ICompanyService;
import com.erp.erp_servers.Customer.ICustomerService;
import com.erp.erp_servers.InspectCargo.IInspectCargoService;
import com.erp.erp_servers.ShipmentPlan.*;
import com.erp.erp_servers.User.IUserService;
import com.erp.erp_servers.business.ICarryDetailProductService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.erp.utils.WordUtils;
import com.erp.vo.SelectVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
@RequestMapping(value = "shipmentPlan")
@Api(tags = "出运计划")
public class ShipmentPlanControl extends Base {
    @Autowired
    private IShipmentPlanService shipmentPlanService;
    @Autowired
    private IShipmentPlanProductService shipmentPlanProductService;
    @Autowired
    private IShipmentPlanFileService shipmentPlanFileService;
    @Autowired
    private IShipmentPlanCostService shipmentPlanCostService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IShipmentPlanLogService shipmentPlanLogService;
    @Autowired
    IBankService bankService;
    @Autowired
    IInspectCargoService inspectCargoService;

    @Autowired
    ICarryDetailProductService carryDetailProductService;

    /**
     * 新增出运计划
     *
     * @param shipmentPlan
     * @return 操作记录数
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增出运计划", notes = "新增出运计划", httpMethod = "POST")
    public R<Integer> insert(@RequestBody ShipmentPlan shipmentPlan) {
        try {

            Object cacheUser = this.getBean("user");

            if (StringUtils.isEmpty(shipmentPlan.getShipmentNo())) {
                return R.buildError(ResultStatusEnum.NOT_EMPTY);
            }
            if (null == shipmentPlan.getId()) {
                boolean isExist = shipmentPlanService.getCount(shipmentPlan.getShipmentNo());
                if (isExist) {
                    return R.buildError(ResultStatusEnum.BILL_NO_REPEAT);
                }
            }
            int num=0;
            UserEntity user = (UserEntity) cacheUser;
           if(null != shipmentPlan.getId()){

               shipmentPlan.setExecutorCode(user.getCode());
               //shipmentPlan.setBoxStatus("1");
               shipmentPlan.setEditTime(new Date(System.currentTimeMillis()));
                num = shipmentPlanService.updateByPrimaryKey(shipmentPlan);
          }else{

              shipmentPlan.setExecutorCode(user.getCode());
              //shipmentPlan.setBoxStatus("1");
              shipmentPlan.setCreateTime(new Date(System.currentTimeMillis()));
               num = shipmentPlanService.insert(shipmentPlan);
          }

            return R.build(num);

        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id查询出运计划
     *
     * @param id
     * @return 出运计划实体
     */
    @RequestMapping(value = "selectByKey")
    @ResponseBody
    public R<ShipmentPlan> selectByKey(Integer id) {
        try {
            ShipmentPlan shipmentPlan = shipmentPlanService.selectByPrimaryKey(id);
            return R.build(shipmentPlan);
        } catch (NumberFormatException e1) {
            e1.printStackTrace();
            return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id更新出运计划
     * 部分字段
     *
     * @param shipmentPlan
     * @return 操作记录数
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "按id更新出运计划", notes = "按id更新出运计划", httpMethod = "POST")
    public R<Integer> update(@RequestBody ShipmentPlan shipmentPlan) {
        try {
            Object cacheUser = this.getBean("user");
            if (null != cacheUser) {
                UserEntity user = (UserEntity) cacheUser;
                shipmentPlan.setExecutorCode(user.getCode());
                shipmentPlan.setEditTime(new Date(System.currentTimeMillis()));
                int num = shipmentPlanService.updateByPrimaryKey(shipmentPlan);
                return R.build(num);
            } else {
                return R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据主键更新状态
     *
     * @return
     */
    @RequestMapping(value = "updateByKey")
    @ResponseBody
    public R updateByKey(String id, String statuss) {
        try {
            UserEntity user = (UserEntity)this.getBean("user");

            int num=0;
            ShipmentPlanVo vo = new ShipmentPlanVo();
            vo.setId(Long.parseLong(id));
            vo.setBoxStatus(statuss);
                num = shipmentPlanService.updateBoxStatusByKey(vo,user.getCode());
            return R.build(num);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }
    /**
     * 根据主键更新状态
     *
     * @return
     */
    @RequestMapping(value = "updateByKey2")
    @ResponseBody
    public R updateByKey2(String id, String statuss) {
        try {
            UserEntity user = (UserEntity)this.getBean("user");

            int num=0;
            ShipmentPlanVo vo = new ShipmentPlanVo();
            vo.setId(Long.parseLong(id));
            vo.setBoxStatus(statuss);

            ShipmentPlan shipmentPlan = shipmentPlanService.selectByPrimaryKey(Integer.parseInt(id));

            //判断下一环节（验货）有没有验货单存在
           // boolean b = inspectCargoService.selectInspectCargoListByShipmentNo(shipmentPlan.getShipmentNo());
            //判断出运明细是否存在
            CarryDetailProductEntity carryDetailProduct=new CarryDetailProductEntity();
            carryDetailProduct.setValid(1);
            carryDetailProduct.setCarryPlanNo(shipmentPlan.getShipmentNo());
            List<CarryDetailProductEntity> carryDetailProductEntities = carryDetailProductService.queryAll(carryDetailProduct);
            boolean b=carryDetailProductEntities.size()>0?true:false;
            if(b){
                //不可进行操作（如退回）
                num=-1;
            }else{
                num = shipmentPlanService.updateBoxStatusByKey2(vo,shipmentPlan.getShipmentNo(),user.getCode());
            }
            return R.build(num);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }


    /**
     * 按主键id删除出运计划
     *
     * @param id
     * @return 操作记录数
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    @ApiOperation(value = "按id删除出运计划", notes = "按id删除出运计划")
    public R<Integer> delete(Integer id) {
        try {
            int num = 0;
            num = shipmentPlanService.deleteByPrimaryKey(id);
            return R.build(num);
            /*Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                UserEntity user = (UserEntity )cacheUser;
                ShipmentPlan shipmentPlan = shipmentPlanService.selectByPrimaryKey(id);
                int num = 0;
                if(shipmentPlan!=null){
                    num = shipmentPlanService.deleteByPrimaryKey(id);
                }
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }*/
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 新增出运计划产品记录
     *
     * @param shipmentPlanProduct
     * @return 操作记录数
     */
    @RequestMapping(value = "insertProduct", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增出运计划产品", notes = "新增出运计划产品", httpMethod = "POST")
    public R<Integer> insertProduct(@RequestBody ShipmentPlanProduct shipmentPlanProduct) {
        try {
            Object cacheUser = this.getBean("user");
            if (null != cacheUser) {
                int num = shipmentPlanProductService.insertSelective(shipmentPlanProduct);
                return R.build(num);
            } else {
                return R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        } catch (DataIntegrityViolationException e1) {
            e1.printStackTrace();
            return R.buildError(ResultStatusEnum.DATA_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id查询出运计划产品记录
     *
     * @param id
     * @return 出运计划产品
     */
    @RequestMapping(value = "selectProductByKey/{id}")
    @ResponseBody
    @ApiOperation(value = "按id查询出运计划产品", notes = "按id查询出运计划产品", httpMethod = "POST")
    public R<ShipmentPlanProduct> selectProductByKey(@PathVariable("id") Integer id) {
        try {
            if (id == null) {
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            ShipmentPlanProduct shipmentPlanProduct = shipmentPlanProductService.selectByPrimaryKey(id);
            if (shipmentPlanProduct == null) {
                return R.buildError(ResultStatusEnum.NOT_FOUND);
            }
            return R.build(shipmentPlanProduct);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id更新出运计划-产品
     * 部分字段
     *
     * @param shipmentPlanProduct
     * @return 操作记录数
     */
    @RequestMapping(value = "updateProduct")
    @ResponseBody
    @ApiOperation(value = "按id更新出运计划产品", notes = "按id更新出运计划产品", httpMethod = "POST")
    public R<Integer> updateProduct(@RequestBody ShipmentPlanProduct shipmentPlanProduct) {
        try {
            Object cacheUser = this.getBean("user");
            if (null != cacheUser) {
                int num = shipmentPlanProductService.updateByPrimaryKeySelective(shipmentPlanProduct);
                return R.build(num);
            } else {
                return R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按主键id删除出运计划-产品
     *
     * @param productid
     * @return 操作记录数
     */
    @RequestMapping(value = "deleteProduct")
    @ResponseBody
    public R<Integer> deleteProduct(Integer productid, String type) {
        try {
            Object cacheUser = this.getBean("user");
            if (null != cacheUser) {
                UserEntity user = (UserEntity) cacheUser;
                ShipmentPlanProduct shipmentPlanProduct = shipmentPlanProductService.selectByPrimaryKey(productid);
                int num = 0;
                if (shipmentPlanProduct != null) {
                    num = shipmentPlanProductService.deleteByPrimaryKey(productid, type);
                }
                return R.build(num);
            } else {
                return R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 新增出运计划附件
     *
     * @param inquiryFile
     * @return 操作记录数
     */
    @RequestMapping(value = "insertFile", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增出运计划附件", notes = "新增出运计划附件", httpMethod = "POST")
    public R<Integer> insertFile(@RequestBody ShipmentPlanFile inquiryFile) {
        try {
            Object cacheUser = this.getBean("user");
            if (null != cacheUser) {
                int num = shipmentPlanFileService.insertSelective(inquiryFile);
                return R.build(num);
            } else {
                return R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        } catch (DataIntegrityViolationException e1) {
            e1.printStackTrace();
            return R.buildError(ResultStatusEnum.DATA_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id查询出运计划附件
     *
     * @param id
     * @return 出运计划附件实体
     */
    @RequestMapping(value = "selectFileByKey/{id}")
    @ResponseBody
    @ApiOperation(value = "按id查询出运计划附件", notes = "按id查询出运计划附件", httpMethod = "POST")
    public R<ShipmentPlanFile> selectFileByKey(@PathVariable("id") Integer id) {
        try {
            if (id == null) {
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            ShipmentPlanFile inquiryFile = shipmentPlanFileService.selectByPrimaryKey(id);
            if (inquiryFile == null) {
                return R.buildError(ResultStatusEnum.NOT_FOUND);
            }
            return R.build(inquiryFile);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id更新出运计划-附件
     * 部分字段
     *
     * @param inquiryFile
     * @return 操作记录数
     */
    @RequestMapping(value = "updateFile")
    @ResponseBody
    @ApiOperation(value = "按id更新出运计划", notes = "按id更新出运计划产品记录", httpMethod = "POST")
    public R<Integer> updateFile(@RequestBody ShipmentPlanFile inquiryFile) {
        try {
            Object cacheUser = this.getBean("user");
            if (null != cacheUser) {
                int num = shipmentPlanFileService.updateByPrimaryKeySelective(inquiryFile);
                return R.build(num);
            } else {
                return R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按主键id删除出运计划-附件
     *
     * @param id
     * @return 操作记录数
     */
    @RequestMapping(value = "deleteFile/{id}")
    @ResponseBody
    @ApiOperation(value = "按id删除出运计划附件", notes = "按id删除出运计划附件", httpMethod = "POST")
    public R<Integer> deleteFile(@PathVariable("id") Integer id) {
        try {
            Object cacheUser = this.getBean("user");
            if (null != cacheUser) {
                UserEntity user = (UserEntity) cacheUser;
                ShipmentPlanFile inquiryFile = shipmentPlanFileService.selectByPrimaryKey(id);
                int num = 0;
                if (inquiryFile != null) {
                    num = shipmentPlanFileService.deleteByPrimaryKey(id);
                }
                return R.build(num);
            } else {
                return R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 新增费用
     *
     * @param shipmentPlanCost
     * @return
     */
    @RequestMapping(value = "insertCost", method = RequestMethod.POST)
    @ResponseBody
    public R<Integer> insertCost(@RequestBody ShipmentPlanCost shipmentPlanCost) {
        try {

            int num = shipmentPlanCostService.insertSelective(shipmentPlanCost);
            return R.build(num);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据出运计划id获取费用
     *
     * @param shipmentPlanId
     * @return
     */
    @RequestMapping(value = "selectCostByPage")
    @ResponseBody
    public R<List<ShipmentPlanCost>> selectCostByPage(Integer shipmentPlanId) {
        try {
            if (null == shipmentPlanId) {
                return R.build(new ArrayList<>());
            }
            List<ShipmentPlanCost> shipmentPlanCosts = shipmentPlanCostService.selectCostBySpId(shipmentPlanId);
            return R.build(shipmentPlanCosts);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据出运计划id获取费用
     *
     * @param shipmentPlanId
     * @return
     */
    @RequestMapping(value = "selectCostBySpId")
    @ResponseBody
    public R<List<ShipmentPlanCost>> selectCostBySpId(Integer shipmentPlanId) {
        try {
            if (shipmentPlanId == null) {
                return R.build(null);//R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            List<ShipmentPlanCost> shipmentPlanCosts = shipmentPlanCostService.selectCostBySpId(shipmentPlanId);
            return R.build(shipmentPlanCosts);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据出运Id查询出运计划 -产品
     *
     * @param spId
     * @return 出运计划-产品
     */
    @RequestMapping(value = "selectProductByShipmentPlanId")
    @ResponseBody
    public R<List<ShipmentPlanProduct>> selectProductByShipmentPlanId(Integer spId) {
        try {

            List<ShipmentPlanProduct> list = shipmentPlanProductService.selectProductByShipmentPlanId(spId);
           /* for(int i=0;i<list.size();i++){
                ShipmentPlanProduct spp=list.get(i);
                spp.setSupplierCode(spp.getSupplierName());
            }*/
            return R.build(list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }

    /**
     * 根据采购Id查询出运计划 -产品
     *
     * @param spId
     * @return 出运计划-产品
     */
    @RequestMapping(value = "selectProductBySpId", method = RequestMethod.GET)
    @ResponseBody
    public R<List<ShipmentPlanProduct>> selectProductBySpId(Integer spId) {
        try {

            List<ShipmentPlanProduct> list = shipmentPlanProductService.selectProductBySpId(spId);
            return R.build(list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }

    /**
     * 根据spId查询出运计划-附件
     *
     * @param shipmentPlanId
     * @return 出运计划-附件
     */
    @RequestMapping(value = "selectFileBySpId")
    @ResponseBody

    public R<List<ShipmentPlanFile>> selectFileBySpId(Integer shipmentPlanId) {
        try {
            if (shipmentPlanId == null) {
                return R.build(null);//R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            List<ShipmentPlanFile> list = shipmentPlanFileService.selectFileBySpId(shipmentPlanId);
            if (list == null) {
                return R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }
            return R.build(list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }

    /**
     * 按条件出运计划
     *
     * @return 出运计划集合
     */
    @RequestMapping(value = "selectShipmentPlanListByPage", method = RequestMethod.GET)
    @ResponseBody
    public R selectShipmentPlanListByPage(ShipmentPlanVo vo

          /* String boxStatus,String numberLike,String salesman,String executor,
                                          String signingcompany,String startMakeTime,
                                          String endMakeTime,String startShipmentTime,String endShipmentTime*/) {
        try {


            List<ShipmentPlan> pageInfo = shipmentPlanService.selectShipmentPlanList(vo);
            return R.build(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id更新审核状态
     * 部分字段
     *
     * @param vo
     * @return 操作记录数
     */
    @RequestMapping(value = "updateBoxStatusByKey")
    @ResponseBody
    @ApiOperation(value = "按id更新审核状态", notes = "按id更新审核状态", httpMethod = "POST")
    public R<Integer> updateBoxStatusByKey(@RequestBody ShipmentPlanVo vo) {
        try {
            UserEntity user = (UserEntity)this.getBean("user");
            if (null != user) {
                if (null == vo || null == vo.getId() || null == vo.getBoxStatus()) {
                    return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
                }
                int num = shipmentPlanService.updateBoxStatusByKey(vo,user.getCode());
                return R.build(num);
            } else {
                return R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }


    /**
     * 分页查询 外销合同列表
     *
     * @param vo 查询参数
     * @return
     */
    @RequestMapping(value = "selectExportByPage", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "按条件查询外销合同", notes = "按条件查询外销合同", httpMethod = "GET")
    public R selectExportByPage(ExportVo vo) {
        try {
            if (vo == null) {
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            Date date = new Date(System.currentTimeMillis());
            if (vo.getStartDeliverTime() != null) {
                if (vo.getEndDeliverTime() == null) {
                    vo.setEndDeliverTime(date);
                }
            }
            PageInfo<ExportVo> pageInfo = shipmentPlanProductService.selectExportByList(vo);
            if (pageInfo == null) {
                return R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按exIds查询外销合同-产品列表
     *
     * @param
     * @return
     */
    @RequestMapping(value = "selectExportProductByExIds", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "按exIds查询外销合同-产品列表", notes = "按exIds查询外销合同-产品列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "exIds", value = "外销合同ids"),
    })
    public R selectCarryPlanProductByCarryPlanIds(@RequestBody Integer[] exIds) {
        try {
            if (null == exIds || exIds.length == 0) {
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            PageInfo<ShipmentPlanProduct> pageInfo = shipmentPlanProductService.selectExportProductByExIds(Arrays.asList(exIds));
            return R.build(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据外销合同号查询采购合同
     *
     * @return
     */
    @RequestMapping(value = "selectCaiGuoByWaiXiao")
    @ResponseBody
    public R<List<PurchaseProductVO>> selectCaiGuoByWaiXiao(String salesContractNo) {
        // List<String> list1 = JSON.parseArray(pcodandgcode, String.class);
        List<PurchaseProductVO> purchaseByExportContractNo = shipmentPlanProductService.getPurchaseByExportContractNo(salesContractNo);
        /*List<PurchaseProductVO> list=new ArrayList<>();
        for(int i=0;i<purchaseByExportContractNo.size();i++){
            PurchaseProductVO vo = purchaseByExportContractNo.get(i);
            for(int n=0;n<list1.size();n++){
                if((vo.getProductNo()+vo.getSpecNo()).equals(list1.get(n))){
                    list.add(vo);
                    break;
                }
            }

        }*/
        return R.build(purchaseByExportContractNo);
    }

    /**
     * 获陬所有客户
     *
     * @return
     */
    @RequestMapping(value = "getCustomer")
    @ResponseBody
    public R getCustomer() {
        try {
            CustomerExample example = new CustomerExample();
            CustomerExample.Criteria criteria = example.createCriteria();
            List<CustomerEntity> customerEntities = customerService.selectByExample(example);

            return R.build(customerEntities);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 获陬所有公司
     *
     * @return
     */
    @RequestMapping(value = "getCompany")
    @ResponseBody
    public R getCompany() {
        try {
            List<SelectVo> allList = companyService.getAllList();
            return R.build(allList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 获陬所有客户
     *
     * @return
     */
    @RequestMapping(value = "getAllUser")
    @ResponseBody
    public R getAllUser() {
        try {
            List<UserEntity> allUserNameAndCode = userService.getAllUserNameAndCode();
            return R.build(allUserNameAndCode);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据出运id获取出运日志
     * @param shipmentPlanId
     * @return
     */
    @RequestMapping(value = "getLogByPage")
    @ResponseBody
    public R<List<ShipmentPlanLog>> getLogByPage(String shipmentPlanId){
        try {
            if(StringUtils.isEmpty(shipmentPlanId)){
                return R.build(new ArrayList<ShipmentPlanLog>());
            }
            ShipmentPlanLogExample example=new ShipmentPlanLogExample();
            ShipmentPlanLogExample.Criteria criteria = example.createCriteria();
            criteria.andShipmentplanidEqualTo(Integer.parseInt(shipmentPlanId));
            example.setOrderByClause("operationdate desc");
            List<ShipmentPlanLog> shipmentPlanLogs = shipmentPlanLogService.selectByExample(example);
            return R.build(shipmentPlanLogs);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    @RequestMapping(value = "gettest")
    @ResponseBody
    public void gettest( ){
       // shipmentPlanService.getPlanProduct("CESHI212-16" ,1505);
    }

    /**
     *
     * @param response
     * @param code  出运计划号
     */
    @RequestMapping(value = "printToHtml" ,method = RequestMethod.GET)
    @ResponseBody
    public void printToHtml(HttpServletResponse response, String code){

        String modelName="shipmentprod.ftl";

        String modelPath="ftl/shipment";

        Map<String, Object> dataMap=new HashMap<>();


        dataMap=shipmentPlanProductService.selectShipmentByCode(code);

       WordUtils.exportWordByTemplate(dataMap,"I:/bigdate/Tomcat 9.0/webapps/erp/WEB-INF/classes/ftl/shipment",modelName,"D:/test.doc");
      /*  String path= URLDecoder.decode((this.getClass().getResource("/")+modelPath).substring(6)+"/");
     //  WordUtils.exportWordByTemplate(dataMap,path,modelName,response);
        FreemarkerUtil.printToHtml(modelName,dataMap,"I:/bigdate/Tomcat 9.0/webapps/erp/WEB-INF/classes/ftl/shipment",response);*/
    }

    /**
     * 根据签约公司编码获取其下银行信息
     * @param companyCode
     * @return
     */
    @RequestMapping(value = "getBank" )
    @ResponseBody
    public R getBank(String companyCode){
        try {
            BankEntity record=new  BankEntity();
            record.setCompanyCode(companyCode);
            List<BankEntity> bankEntities = bankService.selectByExample(record);
            return R.build(bankEntities);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }



    //******************  统计  ************************************

    /**
     * 出运结汇一览总表 统计
     * @param req
     * @return
     */
    @RequestMapping("shipmentPlanStatistics1ListByPage")
    @ResponseBody
    public R shipmentPlanStatistics1(ShipmentPlanStatistics1Req req) {
        try {
            PageInfo<ShipmentPlanStatistics1Res> pageInfo = shipmentPlanService.shipmentPlanStatistics1(req);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 出运结汇一览总表-导出
     * @param req
     * @return
     */
    @RequestMapping("shipmentPlanStatistics1ListExport")
    @ResponseBody
    public R shipmentPlanStatistics1Export(ShipmentPlanStatistics1Req req) {
        try {
            JSONObject info= shipmentPlanService.shipmentPlanStatistics1Export(req);
            return R.build(info);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 判断出运单号是否存在
     * @param code
     * @return
     */
    @RequestMapping("shipmentPlanCodeIsExit")
    @ResponseBody
    public R shipmentPlanCodeIsExit(String code){
        try {
//            boolean exist = shipmentPlanService.isExist(code);
//            return R.build(exist);
            List<ShipmentPlan> list = shipmentPlanService.findShipmentPlanByCode(code);
            return R.build(list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     *
     * 判断该用户是否拥有 出运计划日志显示(CYJHRZXS001) 角色
     */
    @RequestMapping("isContainsRoleShipPlan")
    @ResponseBody
    public R isContainsRoleShipPlan(){
        try {
            UserEntity user = (UserEntity) this.getBean("user");
            if (user.getRoles().contains("CYJHRZXS001")){
                return R.build(true);
            }else {
                return R.build(false);
            }
        } catch (Exception e) {
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }

    }
    /*
     * 查询是否为营销总监管理员,用于显示提交按钮
     * */
    @GetMapping("/isMarketingDirectorMsn")
    @ResponseBody
    public R isMarketingDirectorMsn(){
        UserEntity user = (UserEntity) this.getBean("user");
        String roles=user.getRoles();
        //财务部编码
        String code="YXZJ003";
        if (StringUtils.isNotEmpty(roles) && roles.contains(code)){
            return R.build(user.getMsn());
        }
        return R.build("否");
    }
    /*
     * 查询是否为营销总监管理员,用于显示提交按钮
     * */
    @GetMapping("/isMsn")
    @ResponseBody
    public R isMsn(){
        UserEntity user = (UserEntity) this.getBean("user");
        return R.build(user.getMsn());
    }
    /*
     * 查询是否为营销总监管理员,用于显示提交按钮
     * */
    @GetMapping("/factoryStatisticsCost")
    @ResponseBody
    public R factoryStatisticsCost(){
        try {
            UserEntity user = (UserEntity) this.getBean("user");
            if (user.getRoles().contains("GCTJJFYFS")){
                return R.build(true);
            }else {
                return R.build(false);
            }
        } catch (Exception e) {
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }

    }

    /**
     * 根据 外销合同号 产品编码 查询该产品的剩余数量
     * @param wxAndBhFlag w:外销，b:备货
     * @param shipmentPlanProductId 出运计划产品的id
     * @param exportCode 外销合同号
     * @param productCode 产品编码
     * @param specNo 规格编码
     * @return 剩余数量
     */
    @RequestMapping("getSurplusAmount")
    @ResponseBody
    public R getSurplusAmount(String wxAndBhFlag,Integer shipmentPlanProductId,Integer id,String exportCode,String productCode,String specNo){
        try {
            Integer num=shipmentPlanService.surplusAmount(wxAndBhFlag,shipmentPlanProductId,id,exportCode, productCode,specNo);
            return R.build(num);
        } catch (Exception e) {
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }

    }

}